package com.huaao.micropolice.root.commons;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;


public class CacheUtils {

	private static CacheManager cacheManager = ((CacheManager) SpringContextHolder.getBean("ECacheManager"));
	
	public static Object get(String cacheName, String key) {
		return get0(cacheName, key);
	}

	private static Object get0(String cacheName, String key) {
		Element element = getCache(cacheName).get(key);
		return element == null ? null : element.getObjectValue();
	}

	public static void put(String cacheName, String key, Object value) {
		put0(cacheName, key, value);
	}

	private static void put0(String cacheName, String key, Object value) {
		Element element = new Element(key, value);
		getCache(cacheName).put(element);
	}

	public static void remove(String cacheName, String key) {
		remove0(cacheName, key);
	}

	private static void remove0(String cacheName, String key) {
		getCache(cacheName).remove(key);
	}

	private static Cache getCache(String cacheName) {
		Cache cache = cacheManager.getCache(cacheName);
		if (cache == null)
			return null;
		else
			return cache;
	}

	public static CacheManager getCacheManager() {
		return cacheManager;
	}
}